<!DOCTYPE html>
<html lang="en">
<head>
    
    <meta charset="utf-8">
    <title>data/transform.js - Documentation</title>
    
    
    <script src="scripts/prettify/prettify.js"></script>
    <script src="scripts/prettify/lang-css.js"></script>
    <!--[if lt IE 9]>
      <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
    <![endif]-->
    <link type="text/css" rel="stylesheet" href="styles/prettify.css">
    <link type="text/css" rel="stylesheet" href="styles/jsdoc.css">
    <script src="scripts/nav.js" defer></script>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>

<input type="checkbox" id="nav-trigger" class="nav-trigger" />
<label for="nav-trigger" class="navicon-button x">
  <div class="navicon"></div>
</label>

<label for="nav-trigger" class="overlay"></label>

<nav >
    
    <h2><a href="index.html">Home</a></h2><h3>Namespaces</h3><ul><li><a href="color.html">color</a><ul class='methods'><li data-type='method'><a href="color.html#.setColorOpacity">setColorOpacity</a></li></ul></li><li><a href="data.html">data</a><ul class='methods'><li data-type='method'><a href="data.html#.getMax">getMax</a></li><li data-type='method'><a href="data.html#.getMin">getMin</a></li><li data-type='method'><a href="data.html#.getTotal">getTotal</a></li><li data-type='method'><a href="data.html#.toArray">toArray</a></li><li data-type='method'><a href="data.html#.toJson">toJson</a></li></ul></li><li><a href="date.html">date</a><ul class='methods'><li data-type='method'><a href="date.html#.dateFormat">dateFormat</a></li><li data-type='method'><a href="date.html#.getDate">getDate</a></li></ul></li><li><a href="is.html">is</a><ul class='methods'><li data-type='method'><a href="is.html#.isIDCard">isIDCard</a></li><li data-type='method'><a href="is.html#.isPhone">isPhone</a></li><li data-type='method'><a href="is.html#.isType">isType</a></li></ul></li></ul>
</nav>

<div id="main">
    
    <h1 class="page-title">data/transform.js</h1>
    

    



    
    <section>
        <article>
            <pre class="prettyprint source linenums"><code>/**
 * 单个值的json数组合并为以数组为值的json
 * 
 * @memberof data
 * 
 * @param {Object} json 单个值的json数组
 * @param {Object} fmt json的key 和 value 字段取值，默认{key:'name',value:'value'}
 * 
 * @returns 合并后的 json 
 * 
 * @example  
 * toArray([{name:'zhangsan',value:10},{name:'lisi',value:20}]);
 * // {name:['zhangsan','lisi'],value:[10,20]}
 */
function toArray(json,fmt) {
    fmt = fmt || {key:'name',value:'value'}
    
    const { isType } = this
    
    if(!isType(json,'Object')){
        console.warn('function toArray() input value should be json')
        return json
    }
    
    let array =  []
    json[fmt.key].forEach((item,index)=>{
        let j = {}
        j[fmt.key] = item 
        j[fmt.value] = json[fmt.value][index] || undefined 
        array.push(j)
    })
    
    return array
}
/**
 *  以数组为值的json拆分为单个值的json数组
 * 
 * @memberof data
 * 
 * @param {Array} array 以数组为值的json
 * 
 * @returns 拆分后的 json数组
 * 
 * @example  
 * toJson({name:['zhangsan','lisi'],value:[10,20]}); 
 * // [{name:'zhangsan',value:10},{name:'lisi',value:20}]
 */
function toJson(array) {
    let json = {}
    const { isType } = this
    
    if(!isType(array,'Array')){
        console.warn('function toArray() input value should be Array')
        return json
    }
    
    array.forEach(item=>{
        for(let i in item){
            if(json[i]){
                json[i].push(item[i])
            }else{
                json[i] = [item[i]]
            }
        }
    })
    
    return json
}
module.exports.toArray = toArray
module.exports.toJson = toJson

</code></pre>
        </article>
    </section>




    
    
</div>

<br class="clear">

<footer>
    Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.6.6</a> on Wed Feb 24 2021 16:40:34 GMT+0800 (中国标准时间) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
</footer>

<script>prettyPrint();</script>
<script src="scripts/polyfill.js"></script>
<script src="scripts/linenumber.js"></script>



</body>
</html>
